package com.stgy.lease.web.api.controller;

import com.stgy.lease.common.redis.RedisPrefixEnum;
import com.stgy.lease.common.result.Result;
import com.stgy.lease.model.domain.PaymentType;
import com.stgy.lease.web.api.service.PaymentTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * ClassName:RoomController
 * Package:com.stgy.lease.web.api.controller
 * Description:
 *
 * @Author:helei
 * @Create:2024/9/6 - 10:34
 */
@Tag(name = "支付方式模块")
@RestController
@RequestMapping("/app/payment")
public class PaymentController {

    @Autowired
    private PaymentTypeService paymentTypeService;

    @Autowired
    private RedisTemplate redisTemplate;

    private String paymentListKey = RedisPrefixEnum.STGY_ADMIN_PREFIX + "payment:list";

    /**
     * http://localhost:5174/admin/payment/list GET
     *
     * @return
     */
    @Operation(summary = "查询所有支付方式")
    @GetMapping("/list")
    public Result<List<PaymentType>> list() {
        List<PaymentType> list = (List<PaymentType>) redisTemplate.opsForValue().get(paymentListKey);
        if (list == null) {
            list = paymentTypeService.list();
            redisTemplate.opsForValue().set(paymentListKey, list, 1, TimeUnit.DAYS);
        }
        return Result.ok(list);
    }

    //http://localhost:8888/app/payment/listByRoomId?id=1830435206989889537 GET
    @Operation(summary = "根据房间id获取可选支付方式列表")
    @GetMapping("/listByRoomId")
    public Result<List<PaymentType>> listByRoomId(@RequestParam Long id) {
        return Result.ok(paymentTypeService.listByRoomId(id));
    }

}
